package cn.autoOffice.controller;

import cn.autoOffice.bean.pojo.BulletinManagement;
import cn.autoOffice.service.BulletinManagementService;
import cn.office.shiro.vo.Result;
import cn.office.shiro.vo.ResultCodeEnum;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("bulletinManagement")
public class BulletinManagementController {

    @Autowired
    private BulletinManagementService bulletinManagementService;

    @GetMapping("/getBulletinManagement")
    public Result<PageInfo<BulletinManagement>> selectBulletinManagement(
            @RequestParam(value = "nameOfAnnouncement", required = false) String nameOfAnnouncement,
            @RequestParam("currentPage") Integer currentPage,
            @RequestParam("pageSize") Integer pageSize) {
        Page<Object> page = PageHelper.startPage(currentPage, pageSize);
        List<BulletinManagement> bulletinManagements = bulletinManagementService.selectBulletinManagement(nameOfAnnouncement);
        PageInfo<BulletinManagement> pageInfo = new PageInfo<>(bulletinManagements);
        pageInfo.setTotal(page.getTotal());
        return new Result<>(bulletinManagements.size() > 0 ?
                ResultCodeEnum.SUCCESS :
                ResultCodeEnum.FAIL, pageInfo);
    }

    @PostMapping("/addBulletinManagement")
    public int addBulletinManagement(BulletinManagement bulletinManagement) {
        return bulletinManagementService.addBulletinManagement(bulletinManagement);
    }

    @PostMapping("/deleteBulletinManagement")
    public int deleteBulletinManagement(int id) {
        return bulletinManagementService.deleteBulletinManagement(id);
    }

}
